package org.xq.mq.component;

import org.xq.mq.model.RedisQueueMessage;
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisChannelPublisher {
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);


    @Resource
    private StringRedisTemplate stringRedisTemplate;
//    private static AtomicInteger count = new AtomicInteger();


    public String send_cctv_message (RedisQueueMessage redisMessage) {
        try {
            stringRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            stringRedisTemplate.setKeySerializer(new StringRedisSerializer());

            stringRedisTemplate.convertAndSend("topic_cctv_news", redisMessage);
            System.out.println("消息发送成功: "+ JSON.toJSONString(redisMessage));
            return "消息发送成功";
        }catch (Exception e) {
            e.printStackTrace();
            return "消息发送失败";
        }
    }


    public String send_bbc_message (RedisQueueMessage redisMessage) {
        try {
            stringRedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            stringRedisTemplate.setKeySerializer(new StringRedisSerializer());

            stringRedisTemplate.convertAndSend("topic_bbc_news", redisMessage);
            System.out.println("消息发送成功: "+JSON.toJSONString(redisMessage));
            return "消息发送成功";
        }catch (Exception e) {
            e.printStackTrace();
            return "消息发送失败";
        }
    }
}
